package com.siecomzg.transport.handler;

import android.os.Bundle;
import android.text.TextUtils;

import com.siecomzg.constconfig.ConstDefine;
import com.siecomzg.proto.Message;
import com.siecomzg.transport.CallbackMap;
import com.siecomzg.transport.CallbackTask;
import com.siecomzg.transport.CommonClient;
import com.siecomzg.transport.DeviceCallback;
import com.siecomzg.transport.SiecomDevice;

import java.util.HashMap;

/**
 * Created on 2017/2/4.
 */

public class OtherHandler extends CommonHandler {
    private static CallbackMap callbackMap = CallbackMap.getInstance();
    @Override
    public void onReceive(CommonClient server, Message.siecomproto msg) {

        String msgid = msg.getMsgid();
        HashMap<String, CallbackTask> callbackTaskHashMap = CallbackMap.getCallbackTaskHashMap();
        if(callbackTaskHashMap.containsKey(msgid)){
            CallbackTask callbackTask = callbackTaskHashMap.get(msgid);
            DeviceCallback callback = callbackTask.getCallback();
            callbackTask.getTimer().cancel();
            boolean isOnMain = callbackTask.isOnMain();
            int respondCmd = msg.getCmd();

            if(respondCmd== ConstDefine.DEVICE_SERIAL_NO_RESPOND) {
                readDeviceSNByRemote(msg, callback, isOnMain);
            }
            if(respondCmd==ConstDefine.CODENAME_RESPOND){

                setCodeNameByRemote(msg, callback, isOnMain);

            }
            if(respondCmd==ConstDefine.GET_CODENAME_RESPOND){
                getCodeNameByRemote(msg, callback, isOnMain);
            }
            if(respondCmd==ConstDefine.RESET_RESPOND){
                reset(msg, callback, isOnMain);
            }
            if(respondCmd==ConstDefine.START_LAUNCHER_RESPOND){
                startLauncher(msg, callback, isOnMain);
            }
            if(respondCmd==ConstDefine.PSAM_TEST_RESPOND){
                testPsam(msg, callback, isOnMain);
            }
            if(respondCmd==ConstDefine.LIGHT_TEST_RESPOND){
                testLight(msg, callback, isOnMain);
            }
            if(respondCmd==ConstDefine.SERIAL_TEST_RESPOND){
                testSerial(msg, callback, isOnMain);
            }
            //检测各个模块信息
            if(respondCmd == ConstDefine.CHECK_MODULE_INFO_RESPOND){
                getCheckModuleInfo(msg,callback,isOnMain);
            }

            //蜂鸣控制  声音控制
            if (respondCmd == ConstDefine.MAKE_BEEP_RESPOND ||
                    respondCmd == ConstDefine.MAKE_VOICE_RESPOND
            ){
                makeBeepRespond(msg,callback,isOnMain);
            }
            //获取电池信息
            if(respondCmd == ConstDefine.GET_BATTARY_INFO_RESPOND){
                getBattaryInfo(msg,callback,isOnMain);
            }

            //获取固件版本
            if(respondCmd == ConstDefine.GET_DEVICE_VERSION_RESPOND){
                getDeviceHDVerison(msg,callback,isOnMain);
            }

            callbackTaskHashMap.remove(msgid);
        }
    }
    private void testSerial(Message.siecomproto msg, DeviceCallback callback, boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);

    }

    /**
     * 获取电池信息
     * @param msg
     * @param callback
     * @param isOnMain
     */
    private void getBattaryInfo(Message.siecomproto msg, DeviceCallback callback, boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            bundle.putString(ConstDefine.data, msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt(ConstDefine.CODE, errno);
            bundle.putString(ConstDefine.MESSAGE, msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);
        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);
    }

    /**
     * 获取设备固件版本
     * @param msg
     * @param callback
     * @param isOnMain
     */
    private void getDeviceHDVerison(Message.siecomproto msg, DeviceCallback callback, boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            bundle.putString(ConstDefine.data, msg.getRespond());
            bundle.putString("data2",msg.getRequest());  //新增加的信息
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt(ConstDefine.CODE, errno);
            bundle.putString(ConstDefine.MESSAGE, msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);
        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);
    }
    /**
     * 获取设备各个模块状态
     * @param msg
     * @param callback
     * @param isOnMain
     */
    private void getCheckModuleInfo(Message.siecomproto msg, DeviceCallback callback, boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            bundle.putString(ConstDefine.data, msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt(ConstDefine.CODE, errno);
            bundle.putString(ConstDefine.MESSAGE, msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);
        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);
    }
    /**
     * 蜂鸣控制返回
     * @param msg
     * @param callback
     * @param isOnMain
     */
    private void makeBeepRespond(Message.siecomproto msg, DeviceCallback callback, boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            bundle.putString(ConstDefine.data, msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt(ConstDefine.CODE, errno);
            bundle.putString(ConstDefine.MESSAGE, msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);
        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);
    }
    private void testLight(Message.siecomproto msg, DeviceCallback callback, boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);

    }

    private void testPsam(Message.siecomproto msg, DeviceCallback callback, boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);

    }
    private void startLauncher(Message.siecomproto msg, DeviceCallback callback, boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);

    }

    private void reset(Message.siecomproto msg, DeviceCallback callback, boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);

    }

    private void setCodeNameByRemote(Message.siecomproto msg,DeviceCallback callback,boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            bundle.putString("data", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);


    }
    private void getCodeNameByRemote(Message.siecomproto msg,DeviceCallback callback,boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            bundle.putString("data", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            String id = msg.getRespond();
            if (TextUtils.isEmpty(id)){
                id = msg.getRequest();
            }
            id = id.replace(" ","");
            bundle.putString("message", id);

            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);


    }
    private void readDeviceSNByRemote(Message.siecomproto msg,DeviceCallback callback,boolean isOnMain) {
        int errno = msg.getError();
        if (errno == 0) {
            Bundle bundle = new Bundle();
            bundle.putString("data", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.SUCCEED, bundle, isOnMain);
        } else {
            Bundle bundle = new Bundle();
            bundle.putInt("code", errno);
            bundle.putString("message", msg.getRespond());
            SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);

        }
        SiecomDevice.deliverResult(callback, SiecomDevice.Result.COMPLETED, new Bundle(), isOnMain);


    }

}
